using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class NoGasMiniGame : MonoBehaviour
{
	public int lvl;

	public int tryNum;

	public RectTransform target;

	public Transform pointer;

	public float maxX = 230f;

	public float targetScaleMultiplier = 0.2f;

	private float pointerSpeed = 1f;

	private float targetStartSizeX = 150f;

	private float targetSizeY = 45f;

	private bool isRunning;

	private float targetCurrSizeX;

	public List<Image> dots;

	public Sprite dotNormal;

	public Sprite dotComplete;

	public void NewGame()
	{
		lvl = 0;
		tryNum = 0;
	}

	private void OnEnable()
	{
		Setup();
	}

	public void Setup()
	{
		SingletonMB<GameManager>.Instance.AnyWindowActive = true;
		pointer.DOLocalMoveX(0f - maxX, 0f);
		targetCurrSizeX = targetStartSizeX - (float)lvl * targetScaleMultiplier * targetStartSizeX;
		target.sizeDelta = new Vector2(targetCurrSizeX, 69f);
		float endValue = Random.Range(0f - maxX + 0.5f * targetCurrSizeX, maxX - 0.5f * targetCurrSizeX);
		target.DOLocalMoveX(endValue, 0f);
		pointer.DOLocalMoveX(maxX, pointerSpeed - (float)lvl * 0.1f).SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo);
		isRunning = true;
		if (dots == null)
		{
			return;
		}
		for (int i = 0; i < dots.Count; i++)
		{
			if (!(dots[i] == null))
			{
				bool flag = i < tryNum;
				dots[i].sprite = (flag ? dotComplete : dotNormal);
			}
		}
	}

	private void Update()
	{
		if (isRunning && Singleton<InputController>.Instance.Action)
		{
			TryShootToGainFuel();
		}
		if (Singleton<InputController>.Instance.Y && SingletonMB<GameManager>.Instance != null)
		{
			SingletonMB<GameManager>.Instance.EndGame();
			SingletonMB<GameManager>.Instance.AnyWindowActive = false;
		}
	}

	public void TryShootToGainFuel()
	{
		isRunning = false;
		pointer.DOKill();
		if (pointer.localPosition.x >= target.localPosition.x - 0.5f * targetCurrSizeX && pointer.localPosition.x <= target.localPosition.x + 0.5f * targetCurrSizeX)
		{
			tryNum++;
			if (tryNum == 3)
			{
				lvl++;
				tryNum = 0;
				SingletonMB<GameManager>.Instance.AnyWindowActive = false;
				SingletonMB<GameManager>.Instance.ContinueAfterMiniGame();
				return;
			}
			Setup();
			if (dots != null)
			{
				int num = tryNum - 1;
				if ((num >= 0 || num < dots.Count) && !(dots[num] == null))
				{
					dots[num].sprite = dotComplete;
				}
			}
		}
		else
		{
			SingletonMB<GameManager>.Instance.EndGame();
			SingletonMB<GameManager>.Instance.AnyWindowActive = false;
		}
	}
}
